//
//  GCSettingViewController.m
//  GoldChampion
//
//  Created by Mac on 2018/4/11.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "GCReportViewController.h"
#import "STTextView.h"
#define MAX_NUM 100
@interface GCReportViewController ()
@property(nonatomic, strong) NSArray                     *dataSouce;
@property(nonatomic, strong) STTextView                     *textView;
@property(nonatomic, strong) STLabel                         *numLable;
@end

@implementation GCReportViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"举报";
    [self.tableView st_autoAdjustAllResponder];
    [self configTableFooter];
    [self configDataSouce];
    [self addNotifacations];
    // Do any additional setup after loading the view.
}
#pragma mark --Notifacation
- (void)addNotifacations{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(UITextViewTextDidChange:)
                                                 name:UITextViewTextDidChangeNotification
                                               object:nil];
}
- (void)UITextViewTextDidChange:(NSNotification*)notify{
    if (notify.object == self.textView) {
        UITextView *textView = (UITextView *)notify.object;
        NSString *toBeString = textView.text;
        NSString *lang = [[UIApplication sharedApplication] textInputMode].primaryLanguage; // 键盘输入模式
        if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，五笔，简体手写
            UITextRange *selectedRange = [textView markedTextRange];
            //获取高亮部分
            UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
            // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
            if (!position) {
                if (toBeString.length > MAX_NUM) {
                    textView.text = [toBeString substringToIndex:MAX_NUM];
                }
                NSInteger num = self.textView.text.length;
                self.numLable.text = [NSString stringWithFormat:@"%ld/200", num];
            }
            // 有高亮选择的字符串，则暂不对文字进行统计和限制
            else{
                
            }
        }
        // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
        else{
            NSInteger num = toBeString.length;
            self.numLable.text = [NSString stringWithFormat:@"%ld/200",num];
            if (toBeString.length >= MAX_NUM) {
                textView.text = [toBeString substringToIndex:MAX_NUM];
            }
        }
    }
}
- (void)configDataSouce{
    
    self.dataSouce = @[
                       [[STTableEasyModel alloc] initWithTextString:@"违法违规"
                                                      accessoryView:nil],
                       [[STTableEasyModel alloc] initWithTextString:@"色情低俗"
                                                      accessoryView:nil],
                       [[STTableEasyModel alloc] initWithTextString:@"标题党，封面党，骗点击"
                                                      accessoryView:nil],
                       [[STTableEasyModel alloc] initWithTextString:@"未成年人不适当行为"
                                                      accessoryView:nil],
                       [[STTableEasyModel alloc] initWithTextString:@"发布垃圾广告"
                                                      accessoryView:nil],
                       [[STTableEasyModel alloc] initWithTextString:@"盗用作品"
                                                      accessoryView:nil],
                       [[STTableEasyModel alloc] initWithTextString:@"泄露我的隐私"
                                                      accessoryView:nil],
                       [[STTableEasyModel alloc] initWithTextString:@"内容令我方案"
                                                      accessoryView:nil],
                       ];
    
    [self.tableView reloadData];
    
}
- (void)configTableFooter{
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = [UIColor whiteColor];
    
    self.textView = [[STTextView alloc] initWithFrame:CGRectMake(16, 10, UIScreenWidth - 32, 144)];
    self.textView.placeholder = @"请输入详细举报理由(必填)";
    self.textView.placeholderColor = SecendTextColor;
    self.textView.backgroundColor = TM_backgroundColor;
    [footer addSubview:self.textView];
    
    self.numLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)
                                              text:@"0/200"
                                         textColor:SecendTextColor
                                              font:12
                                       isSizetoFit:NO
                                     textAlignment:NSTextAlignmentRight];
    self.numLable.st_right = self.textView.width - 16;
    self.numLable.bottom = self.textView.bottom - 5;
    [footer addSubview:self.numLable];
    
    footer.height = self.textView.bottom + 20 + 50;
    self.tableView.tableFooterView = footer;
    
    __weak typeof(self) weakSelf =  self;
    STButton * commitButton = [[STButton alloc] initWithFrame:CGRectMake(0, 1, UIScreenWidth, 48)
                                                     title:@"举报"
                                                titleColor:[UIColor whiteColor]
                                                 titleFont:16
                                              cornerRadius:0
                                           backgroundColor:TM_redColor
                                           backgroundImage:nil
                                                     image:nil];
    commitButton.top = [TMUtils tabBarTop];
    commitButton.contentHorizontalAlignment  = UIControlContentHorizontalAlignmentCenter;
    [commitButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedCommitButton];
    }];
    [self.view addSubview:commitButton];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.dataSouce count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 55;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [TMUtils sectionHeaderWithTitle:@"请选择举报分类，分类越准，处理越快"];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    [cell setSt_tableEasyModel:model];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    STTableEasyModel * model = self.dataSouce[indexPath.row];
    if (model.accessoryView) {
        model.accessoryView = nil;
    }else{
        UIImageView * imagview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 18, 11)];
        imagview.image = [UIImage imageNamed:@"选择"];
        model.accessoryView = imagview;
    }
    [self.tableView reloadData];
    
}
#pragma mark --Action Method
- (void)onSelctedCommitButton{
    
}

@end




